package com.zhu.esyblog.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zhu.esyblog.mapper.ArticleMapper;
import com.zhu.esyblog.pojo.Article;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
public class ThreadService {
    //期望此操作在线程池执行，不会影响主线程
    @Async("taskExecutor")
    public void updateArticleVoCount(ArticleMapper articleMapper, Article article) {
        Integer viewCounts = article.getViewCounts();
        Article articleUpdate=new Article();
        articleUpdate.setViewCounts(viewCounts+1);
        LambdaQueryWrapper<Article> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Article::getId,article.getId());
        //设置一个 为了在多线程环境中 为了线程安全
        queryWrapper.eq(Article::getViewCounts,viewCounts);
        articleMapper.update(articleUpdate,queryWrapper);
        try {
           Thread.sleep(5000);
            System.out.println("更新完成了.....");

        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
}
